import http from "../service/http.js"
import Lodash from "lodash"
export const catrModule = {
	//购物车模块
	state: {

		goodList: [
			// 商品列表
			// {
			//   "commodityName": "string",
			//   "commodityPic": "string",
			//   "count": 0,
			//   "createBy": "string",
			//   "createTime": "2021-01-28T10:03:53.852Z",
			//   "goodsId": 0,
			//   "id": 0,
			//   "marketPrice": 0,
			//   "params": {},
			//   "remark": "string",
			//   "searchValue": "string",
			//   "sellPrice": 0,
			//   "updateBy": "string",
			//   "updateTime": "2021-01-28T10:03:53.852Z",
			//   "userId": 0
			// }
		],
		unShelveGoods:[
			// 下架商品
		]
		
	},
	mutations: {
		setGoodList(state, goodList) {
			state.goodList = goodList;
		},
		clearGoodList(state, goodList) {
			state.goodList = [];
		},
		setUnShelveGoods(state, unShelveGoods) {
			state.unShelveGoods = unShelveGoods;
		},
		clearUnShelveGoods(state, unShelveGoods) {
			state.unShelveGoods = [];
		},
		addGood(state, good) {

			good.check = false
			state.goodList.push(good);


		},
		addNum(state, index) {
			// 加数量
			if (state.goodList[index].count < 100)
				state.goodList[index].count = state.goodList[index].count + 1

		},
		subtractNum(state, index) {
			// 减数量
			if (state.goodList[index].count > 1)
				state.goodList[index].count = state.goodList[index].count - 1
		},

		editGood(state, index, good) {
			state.goodList[index] = good;
		},
		removeGood(state, index) {
			state.goodList.splice(index, 1);
		},
		clearCheckGood(state) {
			// 清除已选中的商品

			state.goodList = state.goodList.map((item, index) => {
				if (item.check == true)
					return item
			})
		}

	},
	// getters: {

	// },
	actions: {

		addCatr: async function({
			// 添加购物车
			commit,
			dispatch,
			state
			// goodInfo:{

			// }
		}, params) {
			// console.log("params", params)


			return await new Promise((resolve, reject) => {

				http.post('/system/cart/add', params).then(res => {

					console.log("添加购物车信息成功", res)
					let data = res.data.data
					commit('addGood', params);
					resolve(data)

				}).catch(err => {
					console.log("添加购物车信息失败", err)
					dispatch("getCatrList")
					reject(err)
				})
			})
		},
		editCatr: async function({
			// 编辑购物车数量
			commit,
			dispatch,
			state
			// goodInfo:{

			// }
			// val
		}, params) {
			// console.log("params", params)
			// /system/cart/edit

			return await new Promise((resolve, reject) => {

				http.post('/system/cart/add', params.goodInfo).then(res => {

					console.log("编辑购物车信息成功", res)
					let data = res.data.data
					if (params.index != undefined && params.index != null)
						commit("editGood", params.index, params.goodInfo)
					resolve(data)

				}).catch(err => {
					console.log("编辑购物车信息失败", err)
					dispatch("getCatrList")
					reject(err)
				})
			})
		},
		userAddCatr: async function({
			// 用户编辑购物车数量
			commit,
			dispatch,
			state
			// goodInfo{
			//{
			//   "commodityName": "string",
			//   "commodityPic": "string",
			//   "count": 0,
			//   "createBy": "string",
			//   "createTime": "2021-01-28T10:03:53.852Z",
			//   "goodsId": 0,
			//   "id": 0,
			//   "marketPrice": 0,
			//   "params": {},
			//   "remark": "string",
			//   "searchValue": "string",
			//   "sellPrice": 0,
			//   "updateBy": "string",
			//   "updateTime": "2021-01-28T10:03:53.852Z",
			//   "userId": 0
			// }
		}, params) {
			// 用户添加购物车
			let good = params
			return await new Promise((resolve, reject) => {

				let actGoodIndex = Lodash.findIndex(state.goodList, {
					goodsId: good.goodsId
				})
				console.log("actGoodIndex", actGoodIndex)
				let actGood
				if (actGoodIndex != -1) {
					// 数量加一
					commit("addNum", actGoodIndex)
					actGood = state.goodList[actGoodIndex]
					dispatch("editCatr", {
						goodInfo: actGood
					}).then(res => {
						// 编辑购物车
						console.log("数量加一成功", res)

						resolve(res)
					}).catch(err => {
						// 不成功减一
						commit("subtractNum", actGoodIndex)
						console.log("数量添加不成功", err)
						reject(err)
					})
				} else {
					// 添加商品
					actGood = good
					actGood.check = false
					// state.goodList.push(actGood);
					dispatch("addCatr", actGood).then(res => {
						console.log("加入购物车成功", res)
						resolve(res)

					}).catch(err => {
						console.log("加入购物车不成功", err)
						reject(err)
					})
				}

			})
		},

		getCatrList: async function({
			commit,
			state
		}) {
			// 获取购物车列表
			return await new Promise((resolve, reject) => {

				http.get('/system/cart/list').then(res => {

					console.log("获取购物车列表成功", res)
					let data = res.data.rows
					let goodList=[]
					let unShelveGoods=[]
					data.forEach(item=>{
						if(item.status)
						goodList.push(item)
						else
						unShelveGoods.push(item)
					})
					goodList.map(item => {
						item.check = false
					})
					commit("setGoodList", goodList)
					commit("setUnShelveGoods", unShelveGoods)
					resolve(res)

				}).catch(err => {
					console.log("获取购物车列表失败", err)
					reject(err)
				})
			})
		},
		removeCatr: async function({
			// 删除购物车
			commit,
			dispatch,
			state
			// {
			// ids
			// }
		}, params) {
			// console.log("params", params)
			return await new Promise((resolve, reject) => {

				http.post(`/system/cart/remove/${params.ids}`).then(res => {

					console.log("删除购物车信息成功", res)
					let data = res.data.data

					resolve(data)

				}).catch(err => {
					console.log("删除购物车信息失败", err)
					dispatch("getCatrList")
					reject(err)
				})
			})
		},

		clearCheckGoodAction: async function({
			// 清除支付购物车
			commit,
			dispatch,
			state
			// {
			// ids
			// }
		}) {
			// 清除已选中的商品
			let ids = []
			state.goodList.map((item, index) => {
				if (item.check == true)
					ids.push(item.id)
			})
			commit("clearCheckGood")
			return await new Promise((resolve, reject) => {
				http.post(`/system/cart/remove/${ids.toString()}`).then(res => {

					console.log("清除购物车信息成功", res)
					let data = res.data.data

					resolve(data)

				}).catch(err => {
					console.log("清除购物车信息失败", err)
					dispatch("getCatrList")
					reject(err)
				})
			})

		}

	}
}
